package org.ch00.test08;

import java.io.*;

/*
 *
 * @author XCH
 * @date 2025 - 03 - 07 - 15:25
 *
 */
public class FileCopyExample {
    // 实现文件拷贝的具体逻辑
    public void copyFile(String sourcePath, String destinationPath, FileCopyThread fileCopyThread) throws IOException {
        try (InputStream in = new FileInputStream(sourcePath);
             OutputStream out = new FileOutputStream(destinationPath)) {
            // 定义一个1KB的缓冲区用于读写文件数据
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
                fileCopyThread.updateCopiedBytes(bytesRead);  // 更新已拷贝的字节数
            }
        }
    }
}
